import datetime
import random


class ShoppingReceipt:
    def __init__(self, account_balance):
        self.products = []
        self.shopping_time = datetime.datetime.now()
        self.account_balance = account_balance
        self.total_price = 0

    def buy(self, product, money):
        self.products.append(product)
        self.shopping_time = datetime.datetime.now()
        self.account_balance -= money
        self.total_price += money

    def refund(self, product, money):
        self.products.remove(product)
        self.account_balance += money
        self.total_price -= money


account = 500
goods = {"RTX4070": 100, "RX9070": 50, "14900K": 80, "长城1T固态硬盘": 20, "1000W金牌电源": 60,
         "ROG败家之眼机械键盘": 30, "6000M内存": 45, "雷蛇鼠标": 10, "电竞耳机": 60, "144hz电竞屏": 200}
shoppingCar = []
coupon = {
    1: ["RTX40705折券", 0.5],
    2: ["1000W金牌电源8折券", 0.8],
    3: ["ROG败家之眼机械键盘3折券", 0.3],
    4: ["长城1T固态硬盘9折券", 0.9],
    5: ["RX907075折券", 0.75],
    6: ["14900K6折券", 0.6],
    7: ["6000M内存5折券", 0.5],
    8: ["电竞耳机8折券", 0.8],
    9: ["戴尔鼠标5折券", 0.5],
    10: ["144hz电竞屏6折券", 0.6]
}


def Shopping(account, goods, shoppingCar):
    print("欢迎进入购物商城,新用户免费抽优惠券")
    print("自动抽奖中.....")
    num = random.randint(1, 10)
    print(f"恭喜你抽中了{coupon[num][0]},快去使用吧")
    receipts = ShoppingReceipt(account)
    while True:
        print("商品列表如下:")
        for i in goods.keys():
            print(i + "价格：" + str(goods[i]) + "元")
        print("你想购买还是退款？buy:购买,refund:退款,receipt:打印小票Q/q:退出")
        option = input()
        if option == 'buy':
            good = input('请输入你要购买的商品名称')
            if good not in goods:
                print("没有这个商品，别瞎弄！")
            elif coupon[num][0].startswith(good):
                if account < goods[good] * coupon[num][1]:
                    print('穷鬼，钱不够！请到其他超市购买！')
                else:
                    account -= goods[good] * coupon[num][1]
                    shoppingCar.append(good)
                    receipts.buy(good, goods[good] * coupon[num][1])
                    print(f"你使用{coupon[num][0]}购买了商品：" + good + "价格：" + str(
                        goods[good] * coupon[num][1]) + "元")
            else:
                if account < goods[good]:
                    print('穷鬼，钱不够！请到其他超市购买！')
                else:
                    account -= goods[good]
                    shoppingCar.append(good)
                    receipts.buy(good, goods[good])
                    print("你购买了商品：" + good + "价格：" + str(goods[good]) + "元")
        elif option == 'refund':
            refundgood = input('请输入你要退款的商品')
            if refundgood not in shoppingCar:
                print("你没有购买过这个商品")
            elif coupon[num][0].startswith(refundgood):
                account += goods[refundgood] * coupon[num][1]
                receipts.refund(refundgood, goods[refundgood] * coupon[num][1])
                shoppingCar.remove(refundgood)
                print('退款成功，退款金额为{}'.format(str(goods[refundgood] * coupon[num][1])))
            else:
                account += goods[refundgood]
                shoppingCar.remove(refundgood)
                receipts.refund(refundgood, goods[refundgood])
                print('退款成功，退款金额为{}'.format(str(goods[refundgood])))
        elif option == 'receipt':
            if len(shoppingCar) > 0:
                print("下面是你的购物小票")
                products = receipts.products
                print('购物列表' + str(products))
                print('购物时间' + str(receipts.shopping_time))
                print('消费总额' + str(receipts.total_price))
                print('卡余额' + str(receipts.account_balance))
            else:
                print("你的购物车空空如也，没法打印小票")
        elif option == 'q' or option == "Q":
            print('谢谢惠顾')
            break


Shopping(account, goods, shoppingCar)
